package com.example.demoapp.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.demoapp.R;
import com.example.demoapp.fragment.AudioFragment;
import com.example.demoapp.fragment.InformationFragment;
import com.example.demoapp.fragment.DocumentsFragment;
import com.example.demoapp.fragment.PersonFragment;
import com.example.demoapp.fragment.VideoFragment;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{
    //底部
    private RadioGroup tabmain_bottomNav;
    private RadioButton maintab_1;
    //主页的Fragment管理器
    private FragmentManager fManager;
    private DocumentsFragment documentsFragment;
    private VideoFragment videoFragment;
    private AudioFragment audioFragment;
    private PersonFragment personFragment;
    private InformationFragment informationFragment;
    private SharedPreferences  sharedPreferences;
    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        fManager = getSupportFragmentManager();
        tabmain_bottomNav = (RadioGroup) findViewById(R.id.tabmain_bottomNav);
        tabmain_bottomNav.setOnCheckedChangeListener(this);
        //获取第一个单选按钮，并设置其为选中状态
        maintab_1 = (RadioButton) findViewById(R.id.maintab_1);
        maintab_1.setChecked(true);
     }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction fTransaction = fManager.beginTransaction();
        hideAllFragment(fTransaction);

        switch (checkedId){
            case R.id.maintab_1:
                if(documentsFragment == null){
                    documentsFragment = new DocumentsFragment();
                    fTransaction.add(R.id.ly_content, documentsFragment);
                }else{
                    fTransaction.show(documentsFragment);
                }
                break;
            case R.id.maintab_2:
                if(videoFragment == null){
                    videoFragment = new VideoFragment();
                    fTransaction.add(R.id.ly_content,videoFragment);
                }else{
                    fTransaction.show(videoFragment);
                }
                break;
            case R.id.maintab_3:
                if(audioFragment == null){
                    audioFragment = new AudioFragment();
                    fTransaction.add(R.id.ly_content,audioFragment);
                }else{
                    fTransaction.show(audioFragment);
                }
                break;
            case R.id.maintab_4:
                if(personFragment == null){
                    personFragment = new PersonFragment();
                    fTransaction.add(R.id.ly_content,personFragment);
                }else{
                    fTransaction.show(personFragment);
                }
                break;
            case R.id.maintab_5:
                if(informationFragment == null){
                    informationFragment = new InformationFragment();
                    fTransaction.add(R.id.ly_content,informationFragment);
                }else{
                    fTransaction.show(informationFragment);
                }
                break;
        }
        fTransaction.commit();
    }

    //隐藏所有Fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction){
        if(documentsFragment != null)fragmentTransaction.hide(documentsFragment);
        if(videoFragment != null)fragmentTransaction.hide(videoFragment);
        if(audioFragment != null)fragmentTransaction.hide(audioFragment);
        if(personFragment != null)fragmentTransaction.hide(personFragment);
        if(informationFragment != null)fragmentTransaction.hide(informationFragment);
    }


}